package roborally.Conditions;

import java.util.ArrayList;
import java.util.List;

import be.kuleuven.cs.som.annotate.Basic;
import roborally.Placeable;

/**
 * A class of BoardRange-conditions for playing the roborally game, involving a final maximum X- and Y-coordinate.
 * 
 * @version  2.90
 * @author   Michiel Ruelens & Dieter Naten
 */
public class BoardRange extends Condition {
	
	private final long maxX;
	private final long maxY;
	private static List<BoardRange> brList = new ArrayList<BoardRange>();
	
	/**
	 * Create a new BoardRange condition with given x and y parameters. This condition checks whether a given placeable is within the rectangle
	 * formed by the axes(x=0 and y=0) and the given x and y values, all inclusive.
	 */
	private BoardRange(long x, long y){
		this.maxX = x;
		this.maxY = y;
	}

	/**
	 * Return the maximum X-value of this BoardRange.
	 */
	@Basic
	public long getMaxX() {
		return maxX;
	}

	/**
	 * Return the maximum X-value of this BoardRange.
	 */
	@Basic
	public long getMaxY() {
		return maxY;
	}

	/**
	 * This method returns the BoardRange object. As there is no need to make duplicate objects that have the same parameters;
	 * all existing objects are kept in a list and if an object is required that has the same parameter as one that already exists,
	 * the existing one is returned.
	 */
	public Condition getInstance(long x, long y) {
		for(BoardRange br: brList){
			if(br.getMaxX() == x && br.getMaxY() == y){
				return br;
			}
		}
		BoardRange newBR = new BoardRange(x,y);
		BoardRange.brList.add(newBR);
		return newBR;
	}
	

	/**
	 * This method should not be used. It is required as defined in the class Condition, but this method is replace by the method
	 * getInstance(long x, long y).
	 */
	@Override
	public Condition getInstance() throws IllegalArgumentException {
		System.out.println("You must provide arguments (long x, long y) which dictate the maximum x and y values for the BoardRange.");
		throw new IllegalArgumentException();
	}

	/**
	 * This method returns true if both the X-coordinate and the Y-coordinate of the given placeable
	 * are smaller than or equal to the conditions maxX and maxY, and greater than or equal to 0.
	 */
	@Override
	public boolean validate(Placeable placeable) {
		boolean result = false;
		if(placeable!=null){
			long x = placeable.getX();
			long y = placeable.getY();
			if(x<=maxX && x>=0 && y<=maxY && y>=0){
				result = true;
			}
		}
		return result;
	}


}
